//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-01-05 18:35
 */
//========================================================================

#ifndef _THREADMUTEX_H_
#define _THREADMUTEX_H_
#include "Pre.h"

#include "NDK.h"
#include "TimeValue.h"
#include "GlobalMacros.h"

#include <errno.h>
#if defined NDK_LINUX
#   include <pthread.h>
#   include <sys/time.h>
#elif defined ND_WIN32
#   include <windows.h>
#endif

/**
 * @class ThreadMutex
 *
 * @brief 
 */
class ThreadMutex
{
public:
    ThreadMutex ();
    ~ThreadMutex ();
    //
    int acquire (void);
    
    // acquire lock ownership in <timeout> period 
    int acquire (const TimeValue *timeout);

    // not block if someone already had the lock , errno is EBUSY
    int tryacquire (void);

    // release lock ownership 
    int release (void);

    // return mutex_t
#if defined NDK_LINUX
    const pthread_mutex_t &lock () const;
#elif defined NDK_WIN32
    const CRITICAL_SECTION &lock () const;
#endif
private:
#if defined NDK_LINUX
    pthread_mutex_t mutex_;
#elif defined NDK_WIN32
    CRITICAL_SECTION mutex_;
#endif
};

#include "ThreadMutex.inl"
#include "Post.h"
#endif

